/*医嘱编辑*/

//根据医嘱的CBM01查询医嘱单内容接口

var editAdviceMoudle = (function(){
	function getDetailAdviceByCbm01(cbm01){
		var def = $.Deferred();
		var param = {
				lCBM01: cbm01,
				vaa01: $("#vaa01").val(),
				vaa07: $("#vac01").val(),
				bce01: bce01,
				bck01: bck01,
				acf01: 1,
				lmode: 0
		}
		$.ajax({
			type: "post",
			url: rootUrl+"/cliupdateadvice/clinicadvicedistillupdate",
			data:param,
			dataType: "json",
			success: function (resp) {
				debugger;
				//根据药品vaf01a  == 用法的vaf01  重组数据
				var list = dataGroup(resp.list);
				def.resolve(list);
			}
		});
		return def.promise();
	}
	//医嘱数据分组
	function dataGroup(rList){
		var key = [];
		var vaf01s = [];
		for(var i=0;i<rList.length;i++){
			vaf01s.push(rList[i].vaf01.replace(/-/, ''));
			if(rList[i].vaf01a=='0'){
				key.push(rList[i].vaf01);
			}
		}
		var result =[];
		if(key.length){
			for(var i=0;i<key.length;i++) {
				var vo = {};
				var val = []
				vo.key = key[i];
				for (var j = 0; j < rList.length; j++) {
					if(key[i]==rList[j].vaf01a||key[i]==rList[j].vaf01){
						val.push(rList[j]);
					}
				}
				vo.val = val;
				result.push(vo);
			}
		}
		for(var i=0;i<result.length;i++){
			var kval = result[i].val;
			for(var j=0;j<kval.length;j++){
				var ptype = getProtype(kval[j].bda01a);
				result[i].ptype ={};
				result[i].ptype.beh01 = ptype.beh01;
				result[i].ptype.bda02 = ptype.bda02;
				result[i].ptype.code = ptype.code;
			}
		}
	
		return result;
	};
	//获取药品给药途径医嘱
	function getRouteOrder(rList,data){
		var result = {};
		var vaf01a = data.vaf01a;
		var cbm01 = data.cbm01;
		for(var i=0;i<rList.length;i++){
			var vaf32 = rList[i].vaf32;
			if(vaf32=='1'&&vaf01a==rList[i].vaf01&&cbm01==rList[i].cbm01){
				result = rList[i];
				break;
			}
		}
		return result;
	}
	function editAdviceByType(Icbm01){
		$.when( 
				 getDetailAdviceByCbm01(Icbm01)
	    ).done(function (list) {   //list 里面有药品和给药途径
	     $("#cbm01").val(Icbm01);
	   	 setBackDataToTable(list);
	    })
	}
	//获取中草药煎法
	function getFriedType(rList){
		var result = {};
		for(var i=0;i<rList.length;i++){
			if(rList[i].bda01=='T'){
				result = rList[i];
			}
		}
		return result;
	}
	//获取中草药
	function getDrugData(rList){
		var result = [];
		for(var i=0;i<rList.length;i++){
			if(rList[i].bda01=='3'){
				result.push(rList[i]);
			}
		}
		return result;
	}
	//获取单个药品数据
	function getDrugInfo(bbx01){
		var drugData = {};
		var url = rootUrl+"/clinewadvice/getPtreamprobyid?BBX01="+bbx01;
		$.ajax({
			url: url,
			type: 'GET',
			async: false,
			dataType: 'json',
			success: function (resp) {
				var list = resp.list;
				drugData = list[0]
			}
		});
		return drugData;
	}
	//获取附加手术
	function getOperation(rList){
		var result = [];
		for(var i=0;i<rList.length;i++){
			if(rList[i].bda01=='S'&&rList[i].vaf01a!='0'){
				result.push(rList[i]);
			}
		}
		return result;
	}
	//获取麻醉方式
	function getAnesthesia(rList){
		var result = {};
		for(var i=0;i<rList.length;i++){
			if(rList[i].bda01=='A'){
				result = rList[i];
			}
		}
		return result;
	}
	function getPerson(vaf01){
		var personData = {};
		var Ivaf01 = "-"+vaf01;
		var url = rootUrl+"/cliupdateadvice/getoperatingCrew";
		$.ajax({
			url: url,
			type: 'POST',
			async: false,
			dataType: 'json',
			data:{vaf01:Ivaf01},
			success: function (resp) {
				personData = resp.list;
			}
		});
		return personData;
	}
	//判断数据类型，回填数据
	function setBackDataToTable(rData){  //数据可以识别类别
		var code = rData[0].ptype.code,editArr= [],eArr = [];//eArr 判断是否医保数据要用
		$('#new_order_list a[href="#'+code+'"]').trigger("click");
		for(var i=0;i<rData.length;i++){
			var kval = rData[i].val;
			for(var j=0;j<kval.length;j++){
				if(rData[i].ptype.bda02=='西药'&&kval[j].bda01!='T') {
					var routeObj = getRouteOrder(kval,kval[j]);	//获取给药途径
					var editData = kval[j];
					eArr.push(editData);
					var obj = {};
					obj.edit_data  = editData;
					obj.route_data  = routeObj;
					editArr.push(obj);
				}else if(rData[i].ptype.bda02=='检验'&&kval[j].bda01=='L'){
					editArr.push(kval[j]);
				}
				else if(rData[i].ptype.bda02=='检查'&&kval[j].bda01=='E'&&kval[j].vaf01a=='0'){
					editArr.push(kval[j]);
				}else if(rData[i].ptype.bda02=='中药'&&kval[j].bda01=='8'){
					var routeObj = kval[j]; //中药服用方法
					var friedObj = getFriedType(kval); //中药煎法
					var editData = getDrugData(kval);	//中草药药品
					eArr.push(routeObj);
					var obj = {};
					obj.edit_data  = editData;
					obj.friedObj  = friedObj;
					obj.routeObj  = routeObj;
					editArr.push(obj);
				}else if(rData[i].ptype.bda02=='治疗'&&kval[j].vaf01a=='0'&&(kval[j].bda01=='Z'||kval[j].bda01=='T'||kval[j].bda01=='N'||kval[j].bda01=='O')){
					var editData = kval[j];
					var drugData = getDrugInfo(editData.bbx01);
					var obj = {};
					obj.edit_data  = editData;
					obj.drug_data  = drugData;
					editArr.push(obj);
				}else if(rData[i].ptype.bda02=='材料'){
					editArr.push(kval[j]);
				}else if(rData[i].ptype.bda02=='手术'&&kval[j].bda01=='S'&&kval[j].vaf01a=='0'){
					var editData = kval[j];
					var person = getPerson(editData.vaf01);
					var operations = getOperation(kval); //附加手术
					var anesthesia = getAnesthesia(kval);	//麻醉方式
					var obj = {};
					obj.edit_data  = editData;
					obj.anesthesia  = anesthesia;
					obj.operations  = operations;
					obj.person  = person;
					editArr.push(obj);
				}
			}
		}
		var type = rData[0].ptype.bda02;  //门诊只可能有一种类型，因此用多条数据也用第一个数据判断
		switch(type){
		        case '检验':
		        	assemInspection(editArr);
		            break;
		        case '检查':
		        	assemCheck(editArr);
		        	break;
		        case '西药':
		        	assemWestDrug(editArr,eArr);
		        	break;
		        case '中药':
		        	assemChineseDrug(editArr,eArr);
		        	break;
		        case '手术':
		        	assemOperations(editArr);
		        	break;
		        case '治疗':
		        	assemTreat(editArr);
		        	break;
		        case '材料':
		        	 assemDesc(editArr);	        	
		        	break;
		  }
	}
	//检验
	function assemInspection(editArr){
		for (var i = 0; i < editArr.length; i++)
		{
			addJianyanCyxm(editArr[i]);
		}
	}
	//检查
	function assemCheck(editArr){
		for (var i = 0; i < editArr.length; i++)
		{
			addJianchaCyxm(editArr[i]);
		}
	}
	//西药
	function assemWestDrug(editArr,eArr){//eArr是药品数据，editArr是所有数据
		if (valueArr["p_100_105002_420"]=="1"){  //启用医保用药限制提示
			if (valueArr["p_9999_9999_114"]=="1"){//只针对医保病人
				if($("#patient_bdp02").text()!="自费"){
					editYibaoData1(editArr,eArr);
				}else{
					for (var i = 0; i < editArr.length; i++){
						addXiyaoCyxm(editArr[i]);
					}
				}
			}else{
				editYibaoData1(editArr,eArr);
			}
		}else{
			for (var i = 0; i < editArr.length; i++){
				addXiyaoCyxm(editArr[i]);
			}
		}
	    var  Icbm01 = eArr[0].cbm01;
		editTongzu('',Icbm01);
	}
	//中药
	function assemChineseDrug(editArr,eArr){
		debugger;
		if (valueArr["p_100_105002_420"]=="1"){  //启用医保用药限制提示
			if (valueArr["p_9999_9999_114"]=="1"){//只针对医保病人
				if($("#patient_bdp02").text()!="自费"){
					editZhongyaoYibao(editArr,eArr);
				}else{
					editZhongyaoFeiYibao(editArr,eArr);
				}
			}else{
				editZhongyaoYibao(editArr,eArr);
			}
		}else{
			editZhongyaoFeiYibao(editArr,eArr);
		}
	}
	//手术
	function assemOperations(editArr){
		for (var i = 0; i < editArr.length; i++)
		{
			addShoushuCyxm(editArr[i]);
		}
	}
	//治疗
	function assemTreat(editArr){
		for (var i = 0; i < editArr.length; i++)
		{
			addZhiliaoCyxm(editArr[i]);
		}
	}
	//材料
	function assemDesc(editArr){
		for (var i = 0; i < editArr.length; i++)
		{
			addcailiaoCyxm(editArr[i]);
		}
	}
	//西药医保编辑
	function editYibaoData1(editArr,eArr){
		 $.when( 
				 getVeh02(eArr)
	         ).done(function (list) { 
	        	 debugger
	        	 var editRestult = [],conbindata;
	        	 for(var k =0; k<editArr.length;k++){
	        		 var eobj = {};
	        		 var edata  = editArr[k].edit_data;
	        		 var rdata  = editArr[k].route_data; //给药途径
	        		 for(var i=0;i<list.length;i++){
	        			 if(list[i].vaf01 == edata.vaf01){
	        				 edata = $.extend(edata,list[i]);
	        			 }
	        		 }
	        		 eobj.edit_data = edata;
	        		 eobj.route_data = rdata;
	    			 addXiyaoCyxm(eobj);
	     		 }
	        	 var  Icbm01 = eArr[0].cbm01;
	        	 editTongzu('',Icbm01);
	       });
	}
	//中药医保数据编辑
	function editZhongyaoYibao(editArr,eArr){
		 $.when( 
				 getVeh02(eArr)
	         ).done(function (list) { 
	        	 debugger
	        	 var edata  = editArr[0].edit_data;
				 var friedObj  = editArr[0].friedObj;
				 var routeObj  = editArr[0].routeObj;
	        	 for(var k =0; k<edata.length;k++){
	        		 for(var i=0;i<list.length;i++){
	        			 if(list[i].vaf01 == edata[k].vaf01){
	        				 $.extend(edata[k],list[i]);
	        			 }
	        		 }
	        		 $.extend(edata[k],{'type' : "edit"});
	        		 addZhongyaoCyxm(edata[k]); //药品
	     		 }
	        	 EditFormDataShow(friedObj,routeObj);  //中药用法等form数据
	       });
	}
	//中药非医保数据编辑
	function editZhongyaoFeiYibao(editArr,eArr){
			var edata  = editArr[0].edit_data;
			var friedObj  = editArr[0].friedObj;
			var routeObj  = editArr[0].routeObj;
			for(var k =0; k<edata.length;k++){
				 $.extend(edata[k],{'type' : "edit"});
				addZhongyaoCyxm(edata[k]); //药品
			}
			EditFormDataShow(friedObj,routeObj);  //中药用法等form数据
		
	}
	function getProtype(bda01){
	    var result = {};
	    switch(bda01){
	        case '1':
	            result.beh01 = 1;
	            result.bda02 = '西药';
	            result.code ="Xiyao";
	            break;
	        case '2':
	            result.beh01 = 1;
	            result.bda02 = '西药';
	            result.code ="Xiyao";
	            break;
	        case '8':
	            result.beh01 = 3;
	            result.bda02 = '中药';
	            result.code ="Zhongyao";
	            break;
	        case '3':
	            result.beh01 = 3;
	            result.bda02 = '中药';
	            result.code ="Zhongyao";
	            break;
	        case 'E':
	            result.beh01 = 4;
	            result.bda02 = '检查';
	            result.code ="Jiancha";
	            break;
	        case 'L':
	            result.beh01 = 5;
	            result.bda02 = '检验';
	            result.code ="Jianyan";
	            break;
	        case 'P':
	            result.beh01 = 5;
	            result.bda02 = '检验';
	            result.code ="inspection";
	            break;
	        case 'S':
	            result.beh01 = 6;
	            result.bda02 = '手术';
	            result.code ="Shoushu";
	            break;
	        case 'A':
	            result.beh01 = 6;
	            result.bda02 = '手术';
	            result.code ="Shoushu";
	            break;
	        case 'O':
	            result.beh01 = 7;
	            result.bda02 = '治疗';
	            result.code ="Zhiliao";
	            break;
	        case 'D':
	            result.beh01 = 7;
	            result.bda02 = '治疗';
	            result.code ="Zhiliao";
	            break;
	        case 'N':
	            result.beh01 = 7;
	            result.bda02 = '治疗';
	            result.code ="Zhiliao";
	            break;
	        case 'B':
	            result.beh01 = 7;
	            result.bda02 = '治疗';
	            result.code ="Zhiliao";
	            break;
	        case 'T':
	            result.beh01 = 7;
	            result.bda02 = '治疗';
	            result.code ="Zhiliao";
	            break;
	        case 'Z':
	            result.beh01 = 7;
	            result.bda02 = '治疗';
	            result.code ="Zhiliao";
	            break;
	        case '4':
	            result.beh01 = 8;
	            result.bda02 = '材料';
	            result.code ="Cailiao";
	            break;
	        default:
	            result.beh01 = 0;
	            result.bda02 = '';
	            result.code = '';
	            break;
	    }
	    return result;
	}
	return {
		editAdviceByType:editAdviceByType
	}
}());
